package org.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

public class ClientThread{
	
	private Socket clientSocket;
	private DataOutputStream outputStream;
	private BufferedReader inputStream;
	
	
	public ClientThread(int port)
	{
		
		try {
			clientSocket = new Socket(InetAddress.getLocalHost(),port);
			outputStream = new DataOutputStream(clientSocket.getOutputStream());
			inputStream  = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			new SocketListener().start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void send(String text)
	{
		try {
			outputStream.writeBytes(text);	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private class SocketListener extends Thread
	{
		@Override
		public void run() {		
			super.run();
			try {
				String line;
				while((line = inputStream.readLine()) != null)
				{
					System.out.println(line);
				}	
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
	}
}
